package org.siamnet.java.examples.collections;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.ListIterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

class Test {
    private static boolean runUserInput = false;

    public static void main(String[] args) {
        // Example 000 - Basic string array.
        String str[] = new String[3];
        str[0] = "Joe";
        str[1] = "Foo";
        str[2] = "Bar";
        System.out.println("Example 000: The second entry str[1] = " + str[1]);

        // Example 001 - Basic integer array.
        int[] intArray = new int[3];
        intArray[0] = 2;
        intArray[1] = 4;
        intArray[2] = 8;
        for ( int i = 0; i < intArray.length; i++ ) {
            System.out.println("Example 001: intArray[" + i + "] = " + intArray[i]);
        }

        // Example 002 - Looping over an integer array.
        int[] intArray2 = new int[] {16, 32, 64};
        for ( int i = 0; i < intArray2.length; i++) {
            System.out.println("Example 002: intArray2[" + i + "] = " + intArray2[i]);
        }

        // Example 003 - Looping over an integer array, method 2.
        int[] intArray3 = new int[] {128, 256, 512};
        int cnt = 0;
        for ( int j : intArray3 ) {
            System.out.println("Example 003: Key of intArray3 = " + cnt + " Value of intArray3 = " + j);
            cnt++;
        }

        // Example 004 - Print keys and values of a LinkedHashMap in reverse order. 
        LinkedHashMap<String, String> linkedHashMap = new LinkedHashMap<String, String>();
        linkedHashMap.put("entry1", "Testing entry1");
        linkedHashMap.put("entry2", "Testing entry2");
        linkedHashMap.put("entry3", "Testing entry3");
        ArrayList<String> arrayList = new ArrayList<String>(linkedHashMap.keySet());
        ListIterator<String> iterator = arrayList.listIterator();
        while ( iterator.hasNext() ) {
            iterator.next();
        }
        while ( iterator.hasPrevious() ) {
            String key = iterator.previous();
            String value = linkedHashMap.get(key);
            System.out.println("Example 004: key = " + key + " value = " + value);
        }

        // Example 005 - Looping over an ArrayList
        ArrayList<Integer> arrayList2 = new ArrayList<Integer>();
        arrayList2.add(0, 1024);
        arrayList2.add(1, 2048);
        arrayList2.add(2, 4096);
        int i = arrayList2.get(1);
        System.out.println("Example 005: i = " + i);
        for (Integer j : arrayList2) {
            System.out.println("Example 005: j = " + j);
        }

        // Example 006 - Using a BufferedReader to get user input. Finally block explained.
        if ( runUserInput ) {
            BufferedReader input = new BufferedReader (new InputStreamReader(System.in));
            System.out.print("Example 006: Please enter your name: ");
            try {
                String name = input.readLine();
                int nameLength = name.length();
                if ( nameLength > 0 ) {
                    System.out.println("Example 006: Hello " + name + ". Good to meet you.");
                } else {
                    System.out.println("\nExample 006: Why don't you want to get to know me?");
                }
            } catch ( Exception e ) {
                e.printStackTrace();
            } finally {
                System.out.println("Example 006: finally{} always gets it's code executed.");
            }
        }

        // Example 007 - Using a Comparator to reverse the order of a TreeSet using Collections.
        Comparator comparator = Collections.reverseOrder();
        Set reverseSet = new TreeSet(comparator);
        reverseSet.add("Siam");
        reverseSet.add("Gabe");
        reverseSet.add("Gizmo");
        reverseSet.add("Tyger");
        reverseSet.add("Cleo");
        reverseSet.add("Mozart");
        System.out.println("Example 007: " + reverseSet);
        
        // Example 008 - Basic Map, Set, Iterator while(){} loop.
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("cat1", "Siam");
        map.put("cat2", "Gabe");
        map.put("cat3", "Gizmo");
        map.put("cat4", "Keiko");
        map.put("cat5", "Milly");
        map.put("cat6", "Mozart");
        map.put("cat7", "Herberdal");
        map.put("cat8", "Tyger");
        map.put("cat9", "Bierje");
        map.put("cat10", "Jerry");
        Set set = map.entrySet();
        Iterator iterator2 = set.iterator();
        while ( iterator2.hasNext() ) {
            Map.Entry<String, String> mapEntry = (Map.Entry) iterator2.next();
            String key = (String) mapEntry.getKey();
            String value = (String) mapEntry.getValue();
            System.out.println("Example 008: key = " + key + " value = " + value);
        }
        
    }
}